//
//  AppProSMSCodeTFView.swift
//  XiaoweHealthy
//
//  Created by Riven on 2022/3/17.
//

import UIKit

class AppProSMSCodeTFView: AppProTextFieldBaseView {
    
    lazy var bgView = UIView()
    
    var clickBtnCallback: (() -> Void)?
    private let countDownTimer: RLCountDownTimer = RLCountDownTimer()
    
    override var maxCharacterCount: Int {
        8
    }
    override var kAlphaNum: String {
        "0123456789"
    }
    
    var buttonTitleColor: UIColor = btnBgColor
    var buttonTitle = R.string.localizable.获取验证码()
    var buttonCountDownColor = btnBgColor
    
    var isCodeOk: Bool = false
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        countDownTimer.createTimer { [weak self] in
            guard let self = self else {
                return
            }
            
            if self.countDownTimer.curCount <= 0 {
                self.button.isEnabled = true
                self.button.setTitleColor(self.buttonTitleColor, for: .normal)
                self.button.setTitle(self.buttonTitle, for: .normal)
            } else {
                if let isTimerRunning = self.countDownTimer.timer?.state.isRunning, isTimerRunning {
                    self.button.isEnabled = false
                    self.button.setTitleColor(self.buttonCountDownColor, for: .normal)
                    self.button.setTitle("\(self.countDownTimer.curCount)s", for: .normal)
                }
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func addSubViews() {
        super.addSubViews()
        
        addSubview(bgView)
        
        sendSubviewToBack(bgView)
        
        bgView.isHidden = true
        bgView.backgroundColor = collectionBgColor
        bgView.layer.cornerRadius = 18
        bgView.layer.backgroundColor = collectionBgColor.cgColor
        
        icon.image = R.image.login_smscode_icon()
        
        titleLb.isEnabled = true
        titleLb.isHidden = true
        button.setTitle(R.string.localizable.获取验证码(), for: .normal)
        button.titleLabel?.font = AppFont.harmonyOSSans(ofSize: 19, weight: .regular)
//        button.layer.cornerRadius = 18
//        button.layer.backgroundColor = btnBgColor.cgColor
        button.setTitleColor(btnBgColor, for: .normal)
        
        textField.placeholder = R.string.localizable.请输入验证码()
        textField.setPlaceHolderTextColor(placeHolderTextColor)

        textField.clearButtonMode = .never
        textField.keyboardType = .asciiCapableNumberPad
        textField.delegate = self
    }
    
    override func relayoutSubViews() {
        button.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.width.equalTo(120)
            make.height.equalTo(64)
            make.centerY.equalToSuperview()
        }
        
//        bgView.snp.makeConstraints { make in
//            make.left.equalToSuperview()
//            make.right.equalTo(button.snp.left).offset(-12)
//            make.centerY.equalToSuperview()
//            make.height.equalTo(64)
//        }
        icon.snp.makeConstraints { make in
            make.size.equalTo(24)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().inset(20)
        }
        textField.snp.makeConstraints { make in
            make.left.equalTo(icon.snp.right).offset(18)
            make.right.equalTo(button.snp.left).offset(2)
            make.centerY.equalToSuperview()
            make.height.equalTo(30)
        }
    }
    
    @objc override func clickButton(sender: UIButton) {
        clickBtnCallback?()
    }
    
//    func start() {
//        button.isEnabled = false
//        countDownTimer.start()
//    }
//
//    func stop() {
//        button.isEnabled = true
//        countDownTimer.stop()
//    }
    
    func start() {
        countDownTimer.start()
        
        button.isEnabled = false
    }
    
    func stop() {
        countDownTimer.stop()
        
        setButton(isEnabled: true)
        button.setTitle(buttonTitle, for: .normal)
    }
    
    private func setButton(isEnabled: Bool) {
        if let isTimerRunning = countDownTimer.timer?.state.isRunning, isTimerRunning {
            return
        } else {
            button.isEnabled = isEnabled
            if isEnabled {
                button.setTitleColor(buttonTitleColor, for: .normal)
            } else {
                button.setTitleColor(buttonTitleColor.withAlphaComponent(0.5), for: .normal)
            }

        }
    }
    
    func setButtonColor(isEnabled: Bool) {
        if let isTimerRunning = countDownTimer.timer?.state.isRunning, isTimerRunning {
            return
        } else {
            if isEnabled {
                button.setTitleColor(buttonTitleColor, for: .normal)
            } else {
                button.setTitleColor(buttonTitleColor.withAlphaComponent(0.5), for: .normal)
            }
        }
    }
    
    // MARK: - UITextField
    override func textFieldChanged(sender: UITextField) {
        if textContent.count >= 4 {
            isCodeOk = true
        } else {
            isCodeOk = false
        }
        
        tfChangedCallback?(textContent)
    }
    
}
